# -*- coding: utf-8 -*-
"""
@Project ：PMS_AUTO_TEST 
@File    ：fun_srr_open.py
@Author  ：徐涛
@Date    ：2021/8/27
"""
import allure
from time import sleep
from common.utils import Utils
from config.config import Config
from dz_service.page_elements.el_srr_open import srr_open
from dz_service.page_elements.el_permission_open import permission
from dz_service.function.fun_common import Common
from dz_service.function.fun_service_utils import ServiceUtils
from dz_service.function.fun_home import Home
from dz_service.function.fun_permission import Permission
from common.page import Page


class SrrOpen:

    @allure.step('step:作废BOP业务流水')
    def initial_bop_sql(self, client_id):
        if Config().env == 'test':
            Utils.search_for_param('bop', 'sql/service_sql.json', 'initial_bop_sql', ['8', client_id, '1001'])
            Page.logger.info('作废BOP业务流水成功。', extra={'appname': 'initial_bop_sql'})
        else:
            assert True

    @allure.step('step:客户资料完整性检查')
    def clieninfo_check(self):
        '''
        :param page:
        :return:
        '''
        # 点击首页--权限管理
        Home().click_permission()
        # 完整性检查页
        Page.click(permission, '交易权限管理', '随日融')
        ServiceUtils.assert_element([(srr_open, "完整性检查", "资料完整", "资料完整", "资料完整性校验失败")], log='资料完整。')
        ServiceUtils.assert_element([(srr_open, "完整性检查", "风险测评未过期", "风险测评未过期", "风险测评未过期校验失败")], log='风险测评未过期。')
        ServiceUtils.assert_element([(srr_open, "完整性检查", "证件日期未过期", "证件日期未过期", "证件日期未过期校验失败")], log='证件日期未过期。')
        # assert Page.element(srr_open, "完整性检查", "资料完整").text == "资料完整", "资料完整性校验失败"
        # print("资料完整。")
        # assert Page.element(srr_open, "完整性检查", "风险测评未过期").text == "风险测评未过期", "风险测评未过期校验失败"
        # print("风险测评未过期。")
        # assert Page.element(srr_open, "完整性检查", "证件日期未过期").text == "证件日期未过期", "证件日期未过期校验失败"
        # print("证件日期未过期。")
        Page.logger.info('完整性检查效验成功。', extra={'appname': 'clieninfo_check'})
        # 点击菜单--下一步
        Common().click_next()

    @allure.step('step:页面引导页检查')
    def business_description_check(self):
        '''
        :param page:
        :return:
        '''
        # 页面引导页
        # assert Page.element(srr_open, "业务引导", "业务名称").text == "融资融券随日融业务", "随日融权限开通"
        print("业务名称显示正常。")
        assert Page.element(srr_open, "业务引导", "业务介绍").text == "融资融券随日融业务指基于客户已在公司开通融资融券账户的前提下，公司向客户提供的随借随还的融资服务。"
        print("业务介绍显示正常")
        sleep(0.5)
        Page.logger.info('打开业务说明页成功。', extra={'appname': 'business_description_check'})
        # 点击菜单--立即办理
        Common().click_handle_now()

    @allure.step('step:业务准入检查')
    def business_access_check(self):
        # 业务准入页面
        assert Page.element(srr_open, "业务准入", "权限未开通").text == "未开通随日融业务", "已开通随日融业务"
        print("资料完整。")
        assert Page.element(srr_open, "业务准入", "符合名单").text == "您符合名单办理要求", "您不符合名单办理要求"
        print("风险测评未过期。")
        assert Page.element(srr_open, "业务准入", "未满65周岁").text == "客户未满65周岁", "客户已满65周岁"
        print("客户未满65周岁。")
        assert Page.element(srr_open, "业务准入", "非公司员工").text == "非公司员工", "公司员工"
        print("非公司员工。")
        assert Page.element(srr_open, "业务准入", "前20日日均资产").text == "前20日日均资产达到500000", "前20日日均资产不足50W"
        print("前20日日均资产满50W。")
        assert Page.element(srr_open, "业务准入", "要求风险等级").text == "要求风险等级为稳健型,积极型,激进型,客户的风险等级为激进型,满足条件"
        print("风险等级满足要求。")
        Page.logger.info('打开账户准入性检查成功。', extra={'appname': 'business_access_check'})
        # 点击菜单--下一步
        Common().click_next()

    @allure.step('step:协议签署')
    def sign_agreement(self):
        # 业务准入页面
        Page.click(srr_open, '协议签署', '点击签署')
        Permission().agree(srr_open, '协议签署', '本人已充分阅读')
        Permission().agree(srr_open, '协议签署', '本人已充分阅读')
        Permission().agree(srr_open, '协议签署', '本人已充分阅读')
        Permission().agree(srr_open, '协议签署', '本人已充分阅读')

    @allure.step('step:知识测评')
    def evaluation(self, dict):
        # 开始测评

        sleep(1)
        for i in dict:
            sleep(0.7)
            if dict[i] == "A" or dict[i] == "a":
                Page.el_xpath(f"//*[@for='answer{i}1']").click()
                Page.page_down()

            if dict[i] == "B" or dict[i] == "b":
                Page.el_xpath(f"//*[@for='answer{i}2']").click()
                Page.page_down()

            if dict[i] == "C" or dict[i] == "c":
                Page.el_xpath(f"//*[@for='answer{i}3']").click()
                Page.page_down()

            if dict[i] == "D" or dict[i] == "d":
                Page.el_xpath(f"//*[@for='answer{i}4']").click()
                Page.page_down()

            if dict[i] == "E" or dict[i] == "e":
                Page.el_xpath(f"//*[@for='answer{i}5']").click()
                Page.page_down()
        # 提交测评
        Page.click(srr_open, '知识测评', '确定')

    @allure.step('step:客户签名')
    def sign_name(self):
        sleep(1)
        Page.click(srr_open, '客户签名', '选择签名')
        # 客户签名
        Page.click(srr_open, '客户签名', '客户签名')
        # 提交确认
        Page.click(srr_open, '客户签名', '确定')

    @allure.step('step:短信验证')
    def send_message(self, message_code):
        sleep(3)
        # 勾选已阅读协议
        Page.click(srr_open, '短信验证', '已阅读')
        # 发送验证码
        Page.click(srr_open, '短信验证', '发送短信')
        sleep(3)
        # 输入验证码
        Page.send_key(srr_open, '短信验证', '输入验证码', message_code)
        # 提交确认
        Page.click(srr_open, '短信验证', '提交申请')

    @allure.step('step:提交结果验证')
    def check_srr_result(self, client_id):
        Page.check_page_loading(10)
        assert Page.element(srr_open, "申请结果", "提交成功").text == "提交成功", "页面文字：提交成功 检验失败。"
        Page.logger.info('提交成功。', extra={'appname': 'SrrOpen'})
        if Config().env == 'test':
            search_result = Utils.search_client(client_id)
            Page.logger.info(search_result, extra={'appname': 'check_srr_result'})
            Utils.check_val(client_id, search_result['clientId'], "客户号")
